How do I remove special char " in data and origin value below?
EDIT: I have added comma after blue before origin, the actual issue was how to make "[]" to [] after origin.
I want from this:
{
"data" : "[{
"color": "blue",
"origin": "[{"state" : "USA"}, {"state" : "AFRICA"}]"
}]"
}
To This:
{
"data" : [{
"color": "blue",
"origin": [{"state" : "USA"}, {"state" : "AFRICA"}]
}]
}
const strArr = "[{\"foo\": \"bar\"}]";
const arr = JSON.parse(strArr);
console.log(arr);
if that objects name is say "obj" you can do:
obj.data = JSON.parse(obj.data);
this will convert a valid json string into an array
Edit: if you are just trying to make origin an array instead of a string (assuming valid json) it is probably best you just JSON parse the whole thing then set origin to JSON.parse(origin)
let data = "[{\"color\": \"blue\", \"origin\": \"[{\\\"state\\\": \\\"USA\\\"},{\\\"state\\\": \\\"AFRICA\\\"}]\"}]";
let obj = {
data: data
};
let objData = JSON.parse(obj.data);
objData[0].origin = JSON.parse(objData[0].origin);
console.log(objData);